#define _CRT_SECURE_NO_WARNINGS 1
#include "SeqList.h"

void SLInit(SL* pa)
{
	assert(pa);
	pa->a = (SLDateType*)malloc(sizeof(SLDateType) * 4);
	if (pa->a == NULL)
	{
		printf("malloc error\n");
		exit(-1);
	}
	pa->capacity = 4;
	pa->size = 0;
}

void SLDestroy(SL* pa)
{
	assert(pa);

	free(pa->a);
	pa->a = NULL;
	pa->capacity = pa->size = 0;
}
void SLPrint(SL* pa)
{
	assert(pa);
	for(int i= 0;i<pa->size;i++)
	{
		printf("%d ", pa->a[i]);
	}
	printf("\n");
}

void SLCheckCapacity(SL* pa)
{
	assert(pa);
	if (pa->capacity == pa->size)
	{
		SLDateType* tmp =realloc(pa->a, pa->capacity * sizeof(SLDateType) * 2);
		if (tmp == NULL)
		{
			exit(-1);
		}
		pa->a = tmp;
		pa->capacity *= 2;
	}
	
}

void SLPushBack(SL* pa, SLDateType n)
{
	assert(pa);
	SLCheckCapacity(pa);
	pa->a[pa->size] = n;
	pa->size++;
}

void SLPushFront(SL* pa, SLDateType n)
{
	assert(pa);
	SLCheckCapacity(pa);
	pa->size++;
	int i = pa->size;
	while (i--)
	{
		pa->a[i] = pa->a[i - 1];

	}
	pa->a[0] = n;
	
}

void SLPopBack(SL* pa)
{
	assert(pa);
	assert(pa->size > 0);
	pa->size--;
}

void SLPopFront(SL* pa)
{
	assert(pa);
	assert(pa->size > 0);

	int i = 0;
	while (i<pa->size)
	{
		pa->a[i] = pa->a[i + 1];
		i++;
	}
	pa->size--;

}

int SLFind(SL* pa, SLDateType n)
{
	assert(pa);
	for (int i = 0; i < pa->size; i++)
	{
		if (pa->a[i] == n)
		{
			return i;
		}
	}
	return -1;
}

void SLInsert(SL* pa, int pos, SLDateType n)
{
	assert(pa);
	assert(pos >= 0 && pos <= pa->size);

	pa->size++;
	SLCheckCapacity(pa);
	int end = pa->size;
	while (end >= pos)
	{
		pa->a[end] = pa->a[end - 1];
		end--;
	}
	pa->a[pos] = n;
}

void SLErase(SL* pa, int pos)
{
	assert(pa);
	assert(pos >= 0 && pos <= pa->size);
	int i = pos;
	while (i < pa->size)
	{
		pa->a[i] = pa->a[i + 1];
		i++;
	}
	pa->size--;
}

void SLModify(SL* pa, int pos, int n)
{
	assert(pa->a);
	assert(pos >= 0 && pos < pa->size);

	pa->a[pos] = n;
}